package com.lbb.mvc.activity


import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.lbb.mvc.view.BaseView
import com.lbb.mvc.view.LayoutView
import com.lbb.mvc.view.ProgressDialogController
import com.lbb.mvc.view.StatusController
import com.lbb.utils.ActivityCollectorUtil
import com.lbb.utils.databinding.LayoutEmptyBinding
import com.lbb.utils.databinding.LayoutErrorBinding
import com.lbb.utils.databinding.LayoutLoadingBinding


abstract class BaseActivity<B : ViewBinding> : AppCompatActivity(), BaseView, LayoutView<B> {
    lateinit var b: B
    private val progressDialog = ProgressDialogController()
    private val statusView = StatusController()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        b = getLayoutContent()
        setContentView(b.root)
        ActivityCollectorUtil.addActivity(this)
        progressDialog.onAttach(this)
        addStatusView()
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollectorUtil.removeActivity(this)
    }

    private fun addStatusView() {
        if (isStatusView()) {
            val loading = getLayoutLoading()
            val empty = getLayoutEmpty()
            val error = getLayoutError()
            statusView.onAttach(b, loading, empty, error)
            val params = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            addContentView(loading?.root, params)
            addContentView(empty?.root, params)
            addContentView(error?.root, params)
        }
        showContentView()
    }

    override fun showToast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }

    override fun showTopProgressDialog(s: String?, cancelable: Boolean?) {
        progressDialog.showTopProgressDialog(s, cancelable)
    }

    override fun showCenterProgressDialog(s: String?, cancelable: Boolean?) {
        progressDialog.showCenterProgressDialog(s, cancelable)
    }

    override fun dismissTopProgressDialog() {
        progressDialog.dismissTopProgressDialog()
    }

    override fun dismissCenterProgressDialog() {
        progressDialog.dismissCenterProgressDialog()
    }

    override fun dismissProgressDialog() {
        progressDialog.dismissProgressDialog()
    }

    override fun isStatusView(): Boolean {
        return statusView.isStatusView()
    }


    override fun showContentView() {
        statusView.showContentView()
    }

    override fun showLoadingView() {
        statusView.showLoadingView()
    }

    override fun showEmptyView() {
        statusView.showEmptyView()
    }

    override fun showErrorView(listener: View.OnClickListener?) {
        statusView.showErrorView(listener)
    }


    override fun getLayoutLoading(): ViewBinding? {
        return LayoutLoadingBinding.inflate(layoutInflater)
    }

    override fun getLayoutEmpty(): ViewBinding? {
        return LayoutEmptyBinding.inflate(layoutInflater)
    }

    override fun getLayoutError(): ViewBinding? {
        return LayoutErrorBinding.inflate(layoutInflater)
    }

}